var mmplanId ;
var balance;
//var startMoney = Util.getItem("startMoney");
//var limitMoney = Util.getItem("limitMoney");
//var afterMoney =  Util.getItem("afterMoney");
var startMoney = 0;
var riseMoney=0;
var limitMoney = 0;
var afterMoney = 0;

var Main = function() {
				mui.init({
					swipeBack: false //启用右滑关闭功能
				});
				this.init();
				this.even();
				this.loadData();
			};
			Main.prototype = {
				init: function() {
					mmplanId = Util.requestURL('mmplanId');
				},
				even : function(){
					var _this = this;
					$('#G-go-buy').on('tap',function(){
						_this.goBug();
						return false;
					});
						/**跳转充值页面*/
					$('#btn-toRecharge').on('tap', function() {
							window.location.href = 'recharge.html?balance=' + balance;
						})
				},
				goBug:function(){//立即购买
					var buyMoney = $("#buyMoney").val();
					//校验用户账户金额信息
					var curUserInfo = Util.getLoginUser();
					var validateUrl = server_url + '/creditFlow/financingAgency/availableInvestMoneyPlBidPlan.do';
					var param = {
						memberId:curUserInfo.memberId
					}
					$.getJSON(validateUrl,param,function(retData){
						if(retData.success){
							if(buyMoney==''){
								mui.toast('投标金额不能为空');
								$("#G-go-buy").removeClass('mui-active');
								return false;
							}
						    if(parseFloat(buyMoney)>parseFloat(limitMoney)){
						    	$("#G-go-buy").removeClass('mui-active');
								mui.toast('投标金额不能大于最大投资金额');
								return false;
						    }
							if(parseFloat(buyMoney)<parseFloat(startMoney)){
								$("#G-go-buy").removeClass('mui-active');
							    mui.toast('投标金额不能小于起投金额');
								return false;
							}
					        if(parseFloat(buyMoney)>parseFloat(retData.availableInvestMoney)){
					        	$("#G-go-buy").removeClass('mui-active');
							    mui.toast('投标金额不能大于账户余额');
								return false;
							}
					        if(parseFloat(buyMoney)>parseFloat(afterMoney)){
					        	$("#G-go-buy").removeClass('mui-active');
							    mui.toast('投标金额不能大于可投金额');
								return false;
							}
					        
            	            /*var buyUrl = server_url + '/creditFlow/financingAgency/buyBidplanPlManageMoneyPlan.do?isMobile=1&plBidInfo.userMoney=' + buyMoney+ 
            	            '&plBidInfo.bidId=' + mmplanId + '&investType=' + 2
            	            +'&accountid='+curUserInfo.memberId;
            	            Util.getThirdPayLink(buyUrl);*/
            	            
            				var url = server_url + '/creditFlow/financingAgency/buyBidplanPlManageMoneyPlan.do';
							var param = {
								"isMobile": isMobile,
								"plBidInfo.userMoney": buyMoney,
								"plBidInfo.bidId": mmplanId,
								"investType": 2,
								"accountid": curUserInfo.memberId
							}
							url = UrlHelper.customUrl(UrlHelper.urlEncode(param), url);
							url = encodeURIComponent(url);
							mui.openWindow({"url":"huifu.html?url=" + url + "&module=buybidplan","id":"huifu-page"});
						}
					});
					
				},
				loadData:function(){//加载数据
					//账户余额
					var url = server_url +'/user/mobileaccountBpCustMember.do';
					var param = {
						isMobile : '1',
						memberId : Util.getLoginUser().memberId
					}
					$.getJSON(url,param,function(retData){
						if(retData.success){
							balance = retData.money;
							$('.m-available-money').html(retData.money+'元');
						}
					});
					
					var url1 = server_url + '/creditFlow/financingAgency/getDetailPlManageMoneyPlan.do';
					var param1 = {
						isMobile: 1,
						mmplanId: mmplanId
					};
					//加载数据
					$.getJSON(url1, param1, function(retData) {
						var data = retData.data;
						startMoney=data.startMoney;//起投金额
						riseMoney=data.riseMoney;//递增金额
						limitMoney=Util.safeString(data.limitMoney);//上限金额
						afterMoney=data.afterMoney;//可投金额
						
						//获取起投金额信息
						$('.m-start-money').html(startMoney+'元');
						$('.m-rise-money').html(riseMoney+'元');
						$('.m-limit-money').html(limitMoney+'元');
						$('.m-after-money').html(afterMoney+'元');
					});
					
				}
			};
